package soar.test.thread.queue;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Receiver implements Runnable
{
    private final static Logger logger = LoggerFactory
            .getLogger(Receiver.class);

    private static BlockingQueue<Request> requests = new LinkedBlockingQueue<>();

    public Receiver() throws Exception
    {
    }

    public static void enqueue(Request request)
    {
        requests.offer(request);
    }

    public void run()
    {
        int count=0;
        while (true)
        {
            try
            {
                Request request = requests.poll(100, TimeUnit.MILLISECONDS);
                if (request == null)
                {
                    continue;
                }
                count++;
                logger.info("request:{}, total:{}", request.id, count);
            }
            catch (Exception e)
            {
                logger.error("fail: {}", e);
            }
        }
    }
}
